app.controller('headerCtrl', ['$scope', 'listSve', '$state', 'cookies', '$rootScope', function($scope, listSve, $state, cookies, $rootScope) {
	$rootScope.userName = cookies.getCookie('userName');
	//roleName
	$rootScope.roleName = cookies.getCookie('roleName');
	if($rootScope.userName == 'admin'){
		$rootScope.identification = true;
	}else{
		$rootScope.identification = false;
	}
	
	$scope.user = {};
	$scope.newAccount = function() {
			document.getElementById("confirmNewForm").reset();
			$scope.user.loginName = '';
			$scope.user.loginPwd = '';
			$scope.rPassword = '';
			
			//rolesAll
			listSve.rolesAll().list().then(function(data){
//					console.log(data);
					$scope.role_all = data.res.data;
					$('#modal_confirm').modal('show');
				})
			
		}
		//新建用户
	$scope.confirmNew = function() {
		var oVal = angular.element("#inputRole").val();
			if($scope.newForm.$invalid || oVal == '0') {
				//验证失败
				$scope.submitstate = true;
				$scope.alert({
					show: true,
					Title: '系统提示',
					Message:'请完善信息',
					prevId: '',
					Type: false
				});
				
			} else {
				//验证成功
				//ajax 提交数据 》根据返回值判断成功失败
				//var _form = angular.element("#confirmNewForm").serialize();
				console.log(angular.element("#inputRole").val());
				$scope.user.reloginPwd = $scope.rPassword;
				$scope.user.roleId = oVal;
				console.info($scope.user)
				//提交
				listSve.newSave().oPost($scope.user).then(function(data){
					console.log(data);
					if (data.code == 0) {
						$scope.alert({
							show: true,
							Title: '系统提示',
							Message:'新建成功！',
							prevId: '',
							Type: true
						});
						$scope.calback = function(){
							$('#modal_confirm').modal('hide');
						};
					} else{
						$scope.alert({
							show: true,
							Title: '系统提示',
							Message:data.res.msg,
							prevId: '',
							Type: true
						});
						
					}
				})
			}

		}
		//删除用户
	$scope.dleAccount = function() {
			$('#modal_delete').modal('show');
			listSve.roleList().list().then(function(data) {
				console.log(data);
				$scope.role_list = data.res.data;
			})

		}
		//确定删除用户
	$scope.confirmDelete = function() {
		var aCheckbox = angular.element("#table_delete").find("input[type='radio']");
		var _arr = '';
		var oInput = '';
		angular.forEach(aCheckbox, function(data, index, array) {
			if(data.checked == true) {
				_arr = data.value;
				oInput = data;
			}
		});
		if(_arr.length == 0) {
			$scope.alert({
				show: true,
				Title: '系统提示',
				Message:'请选择后删除',
				prevId: '',
				Type: false
			});
			return;
		} else {
			console.log({
				datas: _arr
			});
			$scope.alert({
				show: true,
				Title: '系统提示',
				Message:'确定删除当前选择？',
				prevId: '',
				Type: true
			});
			$scope.calback=function(){
	        	listSve.delRole().list({id:_arr}).then(function(data){
					$scope.alert({
						show: true,
						Title: '系统提示',
						Message:data.res.msg,
						prevId: '',
						Type: false
					});
					$scope.calback = function(){
						$(oInput).parent().parent().remove();
					};
			    })
	        }
		}
	}

	//注销帐号
	$scope.loginOut = function() {

		cookies.setCookie('userName', ''); //清除cookie
		$state.go('login');

	}

}])